using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class DialogController : MonoBehaviour
{
    public TextMeshProUGUI textCharacterName;
    public TextMeshProUGUI textContent;
    public Image imageAvatar;
    private Sprite lunaAvatar;
    private Sprite nalaAvatar;
    private int dialogIndex = 0;

    private GameObject prefabCandle;
    public GameObject candles;
    public GameObject monsters;

    void Awake()
    {
        this.lunaAvatar = Resources.Load<Sprite>("Sprites/Luna");
        this.nalaAvatar = Resources.Load<Sprite>("Sprites/Nala");
        this.prefabCandle = Resources.Load<GameObject>("Prefabs/Items/Candle");
    }

    void Start()
    {
        
    }

    void OnEnable()
    {
        dialogIndex = 0;
        GetDialogInfo();
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            GetDialogInfo();
        }
    }

    public void ShowDialog()
    {
        gameObject.SetActive(true);
        LunaStatusManager.Instance.LunaState = LunaState.Dialog;
    }

    public void HideDialog()
    {
        gameObject.SetActive(false);
        LunaStatusManager.Instance.LunaState = LunaState.Normal;
    }

    public void GetDialogInfo()
    {
        var currentQuest = QuestSystem.Instance.CurrentQuest;
        if (currentQuest == null)
        {
            // 没领取到任务
            this.HideDialog();
            return;
        }
        
        DialogInfo dialogInfo = null;
        switch (currentQuest.Status)
        {
            case QuestStatus.Available:
                if (currentQuest.BeforeDialogs.TryDequeue(out dialogInfo))
                {
                    RefreshDialog(dialogInfo);
                }
                else
                {
                    if (currentQuest.AcceptIsCompleted)
                    {
                        currentQuest.Status = QuestStatus.Archived;
                        GetDialogInfo();
                    }
                    else
                    {
                        // 开始执行任务
                        currentQuest.Status = QuestStatus.InProgress;
                        if (currentQuest is CollectCandles)
                        {
                            // 打开蜡烛捡拾触发器
                            BoxCollider2D[] colliders = this.candles.GetComponentsInChildren<BoxCollider2D>();
                            foreach (var collider in colliders)
                            {
                                collider.isTrigger = true;
                            }
                        }
                        else if (currentQuest is KillMonsters)
                        {
                            this.monsters.SetActive(true);
                        }
                    }
                    this.HideDialog();
                }
                break;
            case QuestStatus.InProgress:
                if (dialogIndex < currentQuest.DuringDialogs.Count)
                {
                    dialogInfo = currentQuest.DuringDialogs[dialogIndex++];
                    if (dialogInfo != null)
                    {
                        RefreshDialog(dialogInfo);
                    }
                }
                else
                {
                    this.HideDialog();
                }
                break;
            case QuestStatus.Completed:
                if (currentQuest.AfterDialogs.TryDequeue(out dialogInfo))
                {
                    RefreshDialog(dialogInfo);
                }
                else
                {
                    currentQuest.Status = QuestStatus.Archived;
                    GetDialogInfo();
                }
                break;
            case QuestStatus.Archived:
                // 获取新任务
                QuestSystem.Instance.GetQuest();
                if (QuestSystem.Instance.CurrentQuest != null)
                {
                    GetDialogInfo();
                }
                else
                {
                    // 没有任务了
                    this.HideDialog();
                }
                break;
            default:
                // 没有任务了
                this.HideDialog();
                break;
        }
    }

    private void RefreshDialog(DialogInfo dialogInfo)
    {
        this.textCharacterName.text = dialogInfo.CharacterName;
        this.textContent.text = dialogInfo.Content;
        if (dialogInfo.CharacterName == Defines.MainCharacterName)
        {
            this.imageAvatar.sprite = this.lunaAvatar;
        }
        else if (dialogInfo.CharacterName == Defines.Nala)
        {
            this.imageAvatar.sprite = this.nalaAvatar;
        }
        this.imageAvatar.SetNativeSize();
    }
}
